package com.wanmait.recycle.aop;
import java.util.Arrays;

import javax.annotation.Resource;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import com.wanmait.recycle.pojo.Admin;
import com.wanmait.recycle.pojo.Log;
import com.wanmait.recycle.service.LogService;
import com.wanmait.recycle.util.IPUtil;
import com.wanmait.recycle.util.SysContext;

@Component
@Aspect//切面类
public class LogAspect {

	@Resource
	private LogService logService;
	
	//切入点表达式
	//@Pointcut("execution(* com.wanmait.recycle.service.impl..(..)) and !execution(* com.wanmait.recycle.service.impl.LogService.save())")
	 @Pointcut("execution(* com.wanmait.recycle.service.*.*(..)) and !execution(* com.wanmait.recycle.service.impl.LogService.*(..))")
	public void pointcutExpression()
	{
		
	}

	//JoinPoint连接点 表示切入的目标方法
	//returnObj 表示目标方法的返回值
	//pointcut切入点表达式
	@AfterReturning(pointcut="pointcutExpression()",returning="returnObj")
	public void afterReturnLog(JoinPoint joinPoint,Object returnObj)
	{
		
		/*StringBuilder info = new StringBuilder();*/
		
		String className = joinPoint.getTarget().getClass().getSimpleName();
		String methodName = joinPoint.getSignature().getName();
		String args = Arrays.asList(joinPoint.getArgs()).toString();
		/*info.append("类:").append(className);
		info.append(",方法:").append(methodName);
		info.append(",参数:").append(args);*/
		if(returnObj!=null)
		{
			/*String returnType = returnObj.getClass().getName();
			info.append(",返回类型:").append(returnType);*/
			try {
				
				Log log = new Log();
				if("findAdminByNameAndPassword".equals(methodName))
				{
					Admin admin = (Admin)returnObj;
					if(admin!=null) 
					{
						
						log.setAdmin(admin);
						String ip=IPUtil.getIpAddr(SysContext.getRequest());
						log.setIp(ip);
						log.setInfo(admin.getName()+"登录成功!");
						logService.save(log);
					}
				}
				
				
			}
			catch(Exception e) {
				e.printStackTrace();
			}
			
		}
		else {
				try {
				
					Log log = new Log();
					if("insertAdmin".equals(methodName))
					{
						Admin admin = (Admin)SysContext.getSession().getAttribute("admin");
						log.setAdmin(admin);
						log.setIp(SysContext.getRequest().getRemoteAddr());
						log.setInfo("添加成功!");
						logService.save(log);
					}}
					catch(Exception e) {
						e.printStackTrace();
					}
				
				try {
					Log log = new Log();
					if("updateById".equals(methodName))
					{
						Admin admin = (Admin)SysContext.getSession().getAttribute("admin");
						System.out.println(admin.getId());
						log.setAdmin(admin);
						log.setIp(SysContext.getRequest().getRemoteAddr());
						log.setInfo("更改密码成功!");
						logService.save(log);
					}}
					catch(Exception e) {
						e.printStackTrace();
					}
				
				try {
					Log log = new Log();
					if("deleteAdmin".equals(methodName))
					{
						Admin admin = (Admin)SysContext.getSession().getAttribute("admin");
						System.out.println(admin.getId());
						log.setAdmin(admin);
						log.setIp(SysContext.getRequest().getRemoteAddr());
						log.setInfo("删除管理员成功!");
						logService.save(log);
					}}
					catch(Exception e) {
						e.printStackTrace();
					}
				try {
					Log log = new Log();
					if("reAddAdmin".equals(methodName))
					{
						Admin admin = (Admin)SysContext.getSession().getAttribute("admin");
						System.out.println(admin.getId());
						log.setAdmin(admin);
						log.setIp(SysContext.getRequest().getRemoteAddr());
						log.setInfo("恢复管理员成功!");
						logService.save(log);
					}}
					catch(Exception e) {
						e.printStackTrace();
					}
				
				/* UserinfoServiceImpl deleteById*/
				try {
					Log log = new Log();
					if("deleteById".equals(methodName))
					{
						Admin admin = (Admin)SysContext.getSession().getAttribute("admin");
						log.setAdmin(admin);
						log.setIp(SysContext.getRequest().getRemoteAddr());
						if("UserinfoServiceImpl".equals(className))
						{
							log.setInfo("删除用户成功!");
							logService.save(log);
						}
						if("RubbishServiceImpl".equals(className))
						{
							log.setInfo("删除垃圾成功!");
							logService.save(log);
						}
					}}
					catch(Exception e) {
						e.printStackTrace();
					}
				
				/* UserinfoServiceImpl restoreByIds*/
				try {
					Log log = new Log();
					if("restoreById".equals(methodName))
					{
						Admin admin = (Admin)SysContext.getSession().getAttribute("admin");
						log.setAdmin(admin);
						log.setIp(SysContext.getRequest().getRemoteAddr());
						log.setInfo("恢复用户成功!");
						logService.save(log);
					}}
					catch(Exception e) {
						e.printStackTrace();
					}
				
		}
		
		//Admin admin = new Admin();
		//admin.setId(1);
		//admin.setLoginIp("127.0.0.1");
		//admin = (Admin)SysContext.getSession().getAttribute("admin");
		/*System.out.println("1111111111111");
		Admin admin = new Admin();
		admin.setId(6);
		log.setAdmin(admin);
		log.setIp("192.1.1.1");
		log.setInfo("登陆成功");
		logService.save(log);*/
		
	}
	
	
	
}
